<!-- 
  File to create the upload tab
-->
<script type="text/javascript">
function toggleButton (button) {
  if (!this.clicked) {
    if (typeof button.disabled != "undefined"){
      button.disabled = !button.disabled;
    }
    else if (button.clicked) {
      button.clicked = false;
      button.value = button.oldValue;
    }
    else {
      button.clicked = true;
      button.oldValue = button.value;
      button.value = 'DISABLED';
    }
  }
}
</script>
<script type="text/javascript" src="../wp-content/plugins/wp-uploader/ajax.js"></script>
<script type="text/javascript" src="../wp-content/plugins/wp-uploader/add.js"></script>
<?php
  global $wpdb, $table_prefix;
  
  global $selCats;
  global $avGroups;
  
  function wpup_has($in){
    for ( $i = 0 ; $i < count($in) ; $i+=1 )
      if( $in[$i] != "")
        return true;
    return false;
  }
  
  
  
  $Message = "";
  $title = "";
  include_once("uploader_functions.php");
 
  $date = date("Y,m,d");
  
  if($_REQUEST["mode"]=='update'){
    if( (isset( $_POST['title']) && $_POST['title']!="" &&
      wpup_has( $_POST['authors']) && 
      wpup_has( $_POST['keywords']) &&  
      class_exists("userGroups") && ( $_FILES['file']['name'])) 
      || 
      (isset( $_POST['title']) && $_POST['title']!="" &&
      wpup_has( $_POST['authors']) &&
      wpup_has( $_POST['keywords']) &&  
      ( $_FILES['file']['name']) &&
      !class_exists("userGroups") ) ){
        
      $iselCats = $_POST['selCats'];
      
      if( class_exists("userGroups") )
        $groups = $_POST['avGroups'];
       
      //NEXT AVAILABLE id TO ATTRIBUTE A FILE
      $id = wpup_getNextFreeId('document'); 
    
      $ititle = unhtmlentities($_POST['title']);  
      $isummary = unhtmlentities($_POST['summary']);
      $file = $_POST['file'];
      $uploaddir = ABSPATH.'wp-content\\uploads\\';
      $uploaddir = uploader_fix_windows_path($uploaddir);
      $uploaddir .= "/publications/";
      $uploadfile = $uploaddir . basename($_FILES['file']['name']);
      $results = wpup_getDocsOrderByTitle();
      $exists = false;
        
      $iauthors = "";
      $ikeywords = "";
        
      $iiauthors = $_POST['authors'];
      for($i = 0 ; $i < count($iiauthors) ; $i += 1){
        if( $iiauthors[$i] != ""){
          if($i == 0)
            $iauthors .= $iiauthors[$i];
          else{
            $iauthors .= ";";
            $iauthors .= $iiauthors[$i];
          }
        }
      }
      $iikeywords = $_POST['keywords']; 
      for($i = 0 ; $i < count($iikeywords) ; $i += 1){
        if ($iikeywords[$i] != ""){
          if($i == 0)
            $ikeywords .= $iikeywords[$i];
          else{
            $ikeywords .= ";";
            $ikeywords .= $iikeywords[$i];
          }
        }
      }
        
      foreach($results as $res){
        if(!$exists){
          if( $res->fileName == $uploadfile ){
            $exists = true;
          }
        }
      } 
    
      $type = explode('.', $uploadfile);
      $type = $type[count($type)-1];
       
      $uploadfile = explode('.', $uploadfile);
      $size = count($uploadfile);
      $uploadfile[$size]=$uploadfile[$size-1];
      $uploadfile[$size-1] = "_".$id.".";
      $uploadfile = implode("", $uploadfile);
      $fileName = $uploadfile;
       
      if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {} 
       
      // use this to discover the file type
      //print_r($type);
        
      //TO GO TO THE db TO GET THE id OF THE GIVEN type
      $typeId = wpup_getTypeByExtName($type);
          
      //In case of unknown file extension, create the file extension and alert the user.                    
      if (!isset($typeId)){
        //create the file extension and continues
        $typeId = wpup_getNextFreeId('type');
        $path = "";
          
        $wpdb->query("INSERT INTO ".$table_prefix."type (id, name, pathToIcon, extName)
                  VALUES ('".$typeId."', '".$type."', '".$path."','".$type."');");
                   
        $Message = "Upload sucessfull. The file extension is unkown ( <a href='?page=wp-uploader/config.php&action=edit&type=".$typeId."#editFileType'>configure file extension</a> ).";
      }
        
      //insert document
      $wpdb->query("INSERT INTO ".$table_prefix."document (id, title, authors, summary, keywords, filename, date, type, downloads)
                         VALUES ('$id','$ititle','$iauthors','$isummary','$ikeywords','$fileName','$date','$typeId', '0');");
              
      //insert docCat
      $size = count($iselCats);
      for ( $i = 0 ; $i < $size ; $i += 1 ){
        if( $iselCats[$i] != 'on'){
          $wpdb->query("INSERT INTO ".$table_prefix."docCat (id1, id2) VALUES ('$id','$iselCats[$i]');");
        }
        else{
          $cats = wpup_available_categories();
          foreach($cats as $cat){
             $idCats = $cat->id;
             if($iselCats[$idCats]=='on')
               $wpdb->query("INSERT INTO ".$table_prefix."docCat (id1, id2) VALUES ('$id','$idCats');");
          }
        }
      }
      
      if(class_exists("userGroups")) {
        $userGroups = new userGroups();
        $plugName = "wp_uploader";
        $permission = "all";
        $description = "";
        $size =  count($groups);
        for( $i = 0; $i < $size ; $i += 1){
          $userGroups->ugSetAccess($groups[$i], $id, $permission, $plugName, $description );
        }
      }
      $iauthors = "";
      $ikeywords = "";
          
      if($Message == "")
        $Message= "Upload sucessfull.";  
    }
    else{
      if(isset($_POST['title']) && $_POST['title']!="")
        $title = unhtmlentities($_POST['title']);

      if(isset($_POST['summary']) && $_POST['summary']!="")
        $summary = unhtmlentities($_POST['summary']);


      if(isset($_POST['selCats']))
        $selCats = $_POST['selCats'];
    
      if(isset($_POST['avGroups']))
        $avGroups = $_POST['avGroups'];
    
      $var= wpup_available_categories();
        
      $iauthors = $_POST['authors'];
      $ikeywords = $_POST['keywords']; 
      $MessageError= "Upload failed. Please fill all fields marked with *.";
    }   
  }
  
  if ($MessageError){
    echo "<div id='message' class='error fade'>";
    echo "<p><strong>".$MessageError."</strong></p>";
    echo "</div>";
  }  
  
  if ($Message){
    echo "<div id='message' class='updated fade'>";
    echo "<p><strong>".$Message."</strong></p>";
    echo "</div>";
  }
?>
<div class="wrap">
  <h2>Upload</h2>
  <form id="uploadForm" enctype="multipart/form-data" method="post" action="?page=wp-uploader/upload.php&amp;mode=update" onsubmit="toggleButton(getElementById('bt_upload')); return true;">
    <table>
      <tr>
        <td style="vertical-align:top">
          <b>*</b>          &nbsp;
        </td>
        <td style="vertical-align:top">
          <h3 style="display:inline">
            Title: </h3>
        </td>
        <td>
          <input name="title" value="<? echo $title; ?>" id="title" type="text" maxlength="40" style="width: 470px"/>
          <br />
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td style="vertical-align:top">
          <h3  style="display:inline">
            Summary: </h3>
          <br />          (Short description)
        </td>
        <td>
<textarea name="summary" id="summary" style="width: 470px; height: 100px; wrap:on"><? echo stripslashes($summary); ?></textarea>
          <br />
        </td>
      </tr>
      <tr>
        <td style="vertical-align:top">
          <b>*</b>          &nbsp;
        </td>
        <td style="vertical-align:top">
          <h3 style="display:inline">
            Authors: </h3>
          <br />          
        </td>
        <td>
        
        <?

  echo "<div style='display: inline; margin-top: 1px;' >";
  $first = true;
  for( $i = 0 ; $i < count($iauthors) ; $i +=1 ){
    if($first){
      echo "<input type='text' name='authors[]'  size='20' value='".$iauthors[$i]."'/>";
      $first = false;
    }
    else
      echo "<br /><input type='text' name='authors[]'  size='20' value='".$iauthors[$i]."'/>";
  }
  
  if(!count($iauthors)){
    echo "<input type='text' name='authors[]'  size='20' />";
  }
  echo "<span id='moreAuthors'>&nbsp;</span></div>";
  echo "<div style='display: inline; margin-top: 1ex;' id='moreAuthors_link'>
                    <a href='javascript:addNewAuthor();'>Add another author</a>
          </div>";
  ?>
        </td>
      </tr>
      <tr>
        <td style="vertical-align:top">
          <b>*</b>          &nbsp;
        </td>
        <td style="vertical-align:top">
          <h3 style="display:inline">
            Keywords:  </h3>
          <br />         
        </td>
        <td>
<?

  echo "<div style='display: inline; margin-top: 1px;' >";
  $first = true;
  for( $i = 0 ; $i < count($ikeywords) ; $i +=1 ){
    if($first){
      echo "<input type='text' name='keywords[]'  size='20' value='".$ikeywords[$i]."'/>";
      $first = false;
    }
    else
      echo "<br /><input type='text' name='keywords[]'  size='20' value='".$ikeywords[$i]."'/>";
  }
  if(!count($ikeywords)){
     echo "<input type='text' name='keywords[]'  size='20' />";
  }
  
  echo "<span id='moreKeywords'>&nbsp;</span></div>";
  echo "<div style='display:inline;margin-top: 1px;' id='moreKeywords_link'>
          <a href='javascript:addNewKeyWord();'>Add another keyword</a>
        </div>";
  ?>          
          <br />
        </td>
      </tr>
      <tr>
        <td>
          <br />
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td style="vertical-align:top">
          <h3 style="display:inline">Available Categories:</h3>   <br />         (Select one or more)
        </td>
        <td style="vertical-align:top">
        <!--<div  id="texto" name="selCats"-->
          <div id="texto" style="width:300px; max-height:300px; overflow: auto;">
<?
  if(count(wpup_getCats())>0){
    global $selCats;
    if(!isset($selCats))
      $selCats = array();
      wpup_print_cats_scroll($selCats, $addId);
  }
  else{
    echo "     No categories created ";
  }
?>
          </div>
        </td>
      </tr>
      <tr>
        <td>
          <br />
        </td>
      </tr>
<?
  if(class_exists("userGroups")){
      ?>
      <tr>
        <td>
        </td>
        <td style="vertical-align:top">
          <h3 style="display:inline">Groups:</h3>   <br />         (Restrict viewing to...)
        </td>
        <td style="vertical-align:top">
        <!--<div name="avGroups" -->
          <div id="avGroups" style="width:300px; max-height:300px; overflow: auto;">
<?
  $userGroups = new userGroups();
  $groups = $userGroups->getGroups(true);
  //$groups
  if($groups){
    global $avGroups;
    if(!isset($avGroups))
      $avGroups = array();
      wpup_groups($avGroups);
  }
  else{
    echo "No groups created ";
  } 
?>
          </div>
        </td>
      </tr>
      <? } ?>
      <tr>
        <td>
          <br />
        </td>
        <tr>
          <td>
            <b>*</b>
          </td>
          <td>
            <h3 style="display:inline">
              File to upload:</h3>
          </td>
          <td>
            <input type="file" value="<? echo $file; ?>" name="file" size="60" />
          </td>
        </tr>
    </table>
    <br />
    <div class="submit" align=right>
    <!--onclick="toggleButton(this)"-->
      <input type="submit" id="bt_upload" name="value" value="<?php _e('Upload', 'upload') ?>" />
      <input type="reset" value="Reset" />
    </div>
  </form>
  
</div>
